package tsif.tcluster.service.proxy.impl;

import java.lang.reflect.Constructor;

import tsif.tcluster.ITClusterAccessor;
import tsif.tcluster.TClusterContext;
import tsif.tcluster.TClusterMaster;
import tsif.tcluster.loader.ScopeClassLoader;
import tsif.tcluster.loader.ScopeClassLoader.IClassFilter;
import tsif.tcluster.master.TChlusterLocalMaster;
import tsif.tcluster.master.TChlusterRemoteMaster;
import tsif.tcluster.netty.TClusterNettyContext;
import tsif.tcluster.rpc.TClusterRpcCallBackMsg;
import tsif.tcluster.rpc.TClusterRpcCallMsg;
import tsif.tcluster.rpc.TClusterRpcInfoMsg;
import tsif.tcluster.service.ITClusterService;
import tsif.tcluster.service.proxy.TClusterServiceProxy;
import tsif.tcluster.session.TClusterSession;

/**
 * 独立加载器服务节点<br>
 * 
 */
public class TClusterRpcServiceProxyByLoader extends TClusterRpcServiceProxy {
	protected ScopeClassLoader classLoader; // 类加载器(暂时不用)

	public TClusterRpcServiceProxyByLoader(ScopeClassLoader classLoader) {
		super();
		this.classLoader = classLoader;
	}

	/**
	 * 创建服务节点<br>
	 * 
	 * @param serviceProxyClass
	 *            服务代理类
	 **/
	@SuppressWarnings("unchecked")
	public static <T extends TClusterRpcServiceProxyByLoader> T createServiceProxy(Class<T> serviceProxyClass, IClassFilter classFilter, long serviceId, String uniqueName, Class<?> classValue,
			Object... args) throws Exception {
		String cname = classValue.getName();
		ScopeClassLoader classLoader = new ScopeClassLoader(cname, classFilter);

		// 通过加载器创建节点
		Class<?> clazz = classLoader.loadClassBySelf(serviceProxyClass.getName(), false);
		Constructor<?> constructor = clazz.getDeclaredConstructor(new Class<?>[] { ScopeClassLoader.class });
		constructor.setAccessible(true);
		return (T) constructor.newInstance(classLoader);
	}

	/**
	 * 创建服务节点<br>
	 **/
	public static TClusterServiceProxy createServiceProxy(long serviceId, String uniqueName, Class<?> classValue, Object... args) throws Exception {
		return createServiceProxy(TClusterRpcServiceProxyByLoader.class, new ServiceClassFilter(), serviceId, uniqueName, classValue, args);
	}

	@Override
	protected ClassLoader getClassLoader() {
		return (classLoader != null) ? classLoader : super.getClassLoader();
	};

	@Override
	public void recv(TClusterSession session, long serviceId, short code, Object data) throws Exception {
		try {
			super.recv(session, serviceId, code, data);
		} catch (Throwable e) {
			System.err.println("recv error! serviceId=" + serviceId + " code=" + code + " classLoader=" + classLoader);
			e.printStackTrace();
		}
	}

	@Override
	public void destroy() throws Exception {
		super.destroy();

		// destroy classloader
		try {
			if (classLoader != null) {
				classLoader.close();
				classLoader = null;
			}
		} catch (Exception e) {
			System.err.println("destroy classloader error!" + classLoader);
			e.printStackTrace();
		}
	}

	/** 类加载过滤 **/
	public static class ServiceClassFilter extends ScopeClassLoader.ClassFilter {
		/** 包含类(类名), 此外的都交由父加载器加载. **/
		protected Class<?>[] includeClasses;
		/** 排除类(类名), 从包含类中过滤 **/
		protected Class<?>[] excludeClasses;

		/** 包含包名 **/
		protected String[] includePackages;
		/** 排除包名 **/
		protected String[] excludePackages;

		public ServiceClassFilter() {
			includeClasses = new Class<?>[] {
					//
			};
			excludeClasses = new Class<?>[] {
					//
					ScopeClassLoader.class, //
					// ervice
					ITClusterService.class, //
					ITClusterService.IReturn.class, //
					TClusterServiceProxy.class, //
					// accessor
					ITClusterAccessor.class, //
					TClusterCallbacks.CallBack.class, //
					// context
					TClusterContext.class, //
					TClusterNettyContext.class, //
					// master
					TClusterMaster.class, //
					TChlusterRemoteMaster.class, //
					TChlusterLocalMaster.class, //
					TClusterMaster.ContextNode.class, //
					// session
					TClusterSession.class, //
					// rpcmsg
					TClusterRpcCallBackMsg.class, //
					TClusterRpcCallMsg.class, //
					TClusterRpcInfoMsg.class, //

			};
			includePackages = new String[] {
					//
			};
			excludePackages = new String[] {
					//
					// "java.lang.",//
					// "java.util.",//
					// "java.io.",//
					// "java.net.",//
					// "java.sql.",//
					// "java.awt.",//
					// "javax.swing.",//
					// "java.text.",//
					// "java.math.",//
					// "tsif.",//
					// "tsif.tcluster.",//
			};
		}

		@Override
		public boolean filter(ScopeClassLoader loader, String className) {
			// // 包含检测
			// if (checkIsPackage(className, includePackages)) {
			// return true; // 自己加载
			// }
			// if (checkIsClass(className, includeClasses)) {
			// return true; // 自己加载
			// }
			// 过滤检测
			if (checkIsPackage(className, excludePackages)) {
				return false; // 交给父类加载
			}
			if (checkIsClass(className, excludeClasses)) {
				return false; // 交给父类加载
			}
			return true; // 只能自己加载
		}
	}
}
